<?php

class Sherpa_Model_Product extends WSB_Model
{

	/**
	 * Hier finden sich alle Daten aus der Tabelle sherpa_products und
	 * aus Tabellen mit 1:1 Beziehung zu sherpa_products
	 *
	 * @var array
	 */
	protected $_data = array(
		// sherpa_products
		'id' => null,
		'languageId' => null,
		'parentId' => null,
		'articleNumber' => null,
		'eanCode' => null,
		'productName' => null,
		'productLine' => null,
		'productText' => null,
		'features' => null,
		'specifications' => null,
		'colorApplicationId' => null,
		'sizeId' => null,
		'sizeTableId' => null,
		'keywords' => null,
		'sort' => null,
		'available' => null,
		//'availableFrom' => null,
		//'availableTo' => null,
		'availabilityText' => null,
		'active' => null,
		//'activeFrom' => null,
		//'activeTo' => null,
		'actualStock' => null,
		'adminInfo' => null,
		'archived' => null,
		'imported' => null,
		'modified' => null,
		'created' => null,
		'sizeInfo' => null,
		// sherpa_images
		'imageFile' => null,
		'imageId' => null,
		// sherpa_prices
		'price' => null,
		'oldPrice' => null,
		// sherpa_currencies
		'currencyCode' => null,
		// sherpa_sizes
		'size' => null,
		// sherpa_sizeTables
		'sizeTableHtml' => null,

		'icons' => array(),
		'images' => array(),
		'imageVariants' => array(),
		'colorApplications' => array(),
		'children' => array()

	);

	/**
	 * @return boolean
	 */
	public function isLoaded()
	{
		return !empty($this->_data['id']);
	}

	/**
	 * @return boolean
	 */
	public function isParent()
	{
		return empty($this->_data['parentId']);
	}

	/**
	 * @return void
	 */
	public function loadParentProductIcons()
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/IconGateway.php');
		$gateway = new Sherpa_Model_IconGateway();
		$this->_data['icons'] = $gateway->fetchParentProductIcons($this->getId(), $this->getLanguageId());
	}

	/**
	 * @return Sherpa_Model_Icons
	 */
	public function getIcons()
	{
		return new Sherpa_Model_Icons($this->_data['icons']);
	}

	/**
	 * @param string $currencyCode
	 * @return void
	 */
	public function loadChildren($currencyCode)
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/ProductGateway.php');
		$gateway = new Sherpa_Model_ProductGateway();
		$children = $gateway->fetchChildren($this->getId(), $this->getLanguageId());
		$this->_data['children'] = $children;
	}

	/**
	 * @return Sherpa_Model_Products
	 */
	public function getChildren()
	{
		return new Sherpa_Model_Products($this->_data['children']);
	}

	/**
	 * @return void
	 */
	public function loadParentProductSizes()
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/SizeGateway.php');
		$gateway = new Sherpa_Model_SizeGateway();
		$this->_data['sizes'] = $gateway->fetchParentProductSizes($this->getId());
	}

	/**
	 * @return Sherpa_Model_Sizes
	 */
	public function getSizes()
	{
		return new Sherpa_Model_Sizes($this->_data['sizes']);
	}

	/**
	 * @return void
	 */
	public function loadParentProductColorApplications()
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/ColorApplicationGateway.php');
		$gateway = new Sherpa_Model_ColorApplicationGateway();
		$this->_data['colorApplications'] = $gateway->fetchParentProductColorApplications($this->getId());
	}

	/**
	 * @return Sherpa_Model_ColorApplications
	 */
	public function getColorApplications()
	{
		return new Sherpa_Model_ColorApplications($this->_data['colorApplications']);
	}

	/**
	 * @return void
	 */
	public function loadParentProductImages()
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/ImageGateway.php');
		$gateway = new Sherpa_Model_ImageGateway();
		$this->_data['images'] = $gateway->fetchParentProductImages($this->getId(), $this->getLanguageId());
	}

	/**
	 * @return Sherpa_Model_Images
	 */
	public function getImages()
	{
		return new Sherpa_Model_Images($this->_data['images']);
	}

	/**
	 * @return void
	 */
	public function loadParentProductImageVariants()
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/ImageGateway.php');
		$gateway = new Sherpa_Model_ImageGateway();
		$this->_data['imageVariants'] = $gateway->fetchParentProductImageVariants($this->getId());
	}

	/**
	 * @return Sherpa_Model_ImageVariants
	 */
	public function getImageVariants()
	{
		return new Sherpa_Model_ImageVariants($this->_data['imageVariants']);
	}

	/**
	 * @param string $currencyCode
	 * @return void
	 */
	public function loadPrice($currencyCode)
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/PriceGateway.php');
		$gateway = new Sherpa_Model_PriceGateway();
		$price = $gateway->fetchPrice($this->getId(), $currencyCode);
		if($price->isLoaded()) {
			$this->setPrice($price->getPrice());
			$this->setOldPrice($price->getOldPrice());
			$this->setCurrencyCode($price->getCurrencyCode());
		}
	}

	/**
	 * @param boolean
	 * @return array|string
	 */
	public function getAvailabilityInfo($asJson = false)
	{
		$sizes = $this->getSizes()->getIds();
		$colorApplications = $this->getColorApplications()->getIds();

		// Initialisieren des colorToSize-Array
		$colorToSize = array();
		foreach($colorApplications AS $colorApplication) {
			foreach($sizes AS $size) {
				$colorToSize[$colorApplication][$size] = 0;
			}
		}

		// Initialisieren des sizeToColor-Array
		$sizeToColor = array();
		foreach($sizes AS $size) {
			foreach($colorApplications AS $colorApplication) {
				$sizeToColor[$size][$colorApplication] = 0;
			}
		}

		// Verfügbarkeiten der Kindprodukte prüfen
		foreach($this->getChildren() AS $child) {
			if($child->getAvailable() == 1) {
				$childId = $child->getId();
				$sizeId = $child->getSizeId();
				$colorId = $child->getColorApplicationId();
				$colorToSize[$colorId][$sizeId] = 1;
				$sizeToColor[$sizeId][$colorId] = 1;
			}
		}
		$info = array(
			'colors' => $colorApplications,
			'sizes' => $sizes,
			'colorToSize' => $colorToSize,
			'sizeToColor' => $sizeToColor
		);
		if($asJson) {
			return Zend_Json::encode($info);
		} else {
			return $info;
		}

	}

	/**
	 * @param boolean $asJson
	 * @return string|array
	 */
	public function getProductInfo($asJson = false)
	{
		$info = array();
		foreach($this->getChildren() AS $child) {
			// Vererbung Preis
			$price = $child->getPrice();
			$price = empty($price) ? $this->getPrice() : $price;
			$info[] = array(
				$child->getId(),					// 0 -> productId
				$child->getColorApplicationId(),	// 1 -> colorApplicationId
				$child->getSizeId(),				// 2 -> sizeId
				$price,								// 3 -> price
				0,									// 4 -> oldPrice
				(int)$child->getAvailable(),		// 5 -> available
				$child->getavailabilityText()		// 6 -> availabilityText
			);
		}
		if($asJson) {
			return Zend_Json::encode($info);
		} else {
			return $info;
		}
	}

	/**
	 * @param boolean $asJson
	 * @return string|array
	 */
	public function getImagesInfo($asJson = false) {
		$info = array();
		foreach($this->getImages() AS $i) {
			$info[] = array(
				$i->id,		// 0 -> id
				$i->fileName	// 1 -> fileName
			);
		}
		if($asJson) {
			return Zend_Json::encode($info);
		} else {
			return $info;
		}
	}

	/**
	 * @param boolean $asJson
	 * @return string|array
	 */
	public function getImageVariantsInfo($asJson = false) {
		$info = array();
		foreach($this->getImageVariants() AS $iv) {
			$info[] = array(
				$iv->imageId,				// 0 -> imageId
				$iv->colorApplicationId,	// 1 -> colorApplicationId
				$iv->fileName				// 2 -> fileName
			);
		}
		if($asJson) {
			return Zend_Json::encode($info);
		} else {
			return $info;
		}
	}

	/**
	 * Zusammengesetzter Produktname aus ProductName und ProductLine
	 *
	 * @return string
	 */
	public function getFullProductName()
	{
		return trim($this->_data['productName'] . ' ' . $this->_data['productLine']);
	}

	/**
	 * @return string
	 */
	public function getImageFile()
	{
	    return $this->imageFile;
	}

}
